<?php
error_reporting(E_ALL & ~E_NOTICE);
@session_start();

	if(!isset($_SESSION['user_username']) || !isset($_SESSION['user_name']) || !isset($_SESSION['user_surname']) || !isset($_SESSION['position_id'])){
    echo "<script>window.location = '../index.php';</script>";
    exit();
	}

	if(isset($_SESSION['position_id'])){
		if($_SESSION['user_username']!='naruepat') {
		echo "<script>window.location = '../index.php';</script>";
   		exit();
		}
		
		if($_SESSION['position_id']!='2') {
		echo "<script>window.location = '../index.php';</script>";
   		exit();
		}
	}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php include('menu.php'); ?>
</head>
<body>
<?php
include("../connect/connect.php");
	
//ลบข้อมูล
if($_GET["action"]=='add') {

	for($i=0;$i<=count($_POST[check]);$i++){
		$Query = mysql_query("DELETE FROM ".$_POST[check][$i]." ");
	}
	
	header('Location: admin.php');
}

$sale = mysql_query("SELECT sale_id FROM sale");
$saleNum = mysql_num_rows($sale);

$sale_report = mysql_query("SELECT sale_id FROM sale_report");
$sale_report_Num = mysql_num_rows($sale_report);

$sale_report2 = mysql_query("SELECT sale_id FROM sale_report2");
$sale_report2_Num = mysql_num_rows($sale_report2);

$debtor_report = mysql_query("SELECT debtor_report_id FROM debtor_report");
$debtor_report_Num = mysql_num_rows($debtor_report);

$debtor_total = mysql_query("SELECT debtor_total_id FROM debtor_total");
$debtor_total_Num = mysql_num_rows($debtor_total);

$debtor = mysql_query("SELECT debtor_id FROM debtor");
$debtor_Num = mysql_num_rows($debtor);

$bill = mysql_query("SELECT bill_id FROM bill");
$bill_Num = mysql_num_rows($bill);
?>
<form action="?action=add" name="formadd" method="post" id="formadd">
<div class="box">
  <div class="left"></div>
  <div class="right"></div>
    <div class="heading">
      <h1>จัดการฐานข้อมูล</h1>
    <div class="buttons"><a onclick="JavaScript:if(confirm('คุณต้องการยืนยันการล้างข้อมูล ใช่หรือไม่?')==true){formadd.submit();}" class="button"><span>ล้างข้อมูล</span></a><a onclick="location = 'index.php';" class="button"><span>ยกเลิก</span></a></div></div>
  <div class="content">
  <table class="list">
        <thead>
          <tr>
          	<td class="center" width="10%">เลือก</td>
          	<td class="center">ชื่อฐานข้อมูล</td>
          </tr>
        </thead>
        <tbody>
        <tr>
        <td class="center"><input type="checkbox" name="check[]" value="sale" /></td>
        <td class="left" style="padding-left: 20px;">sale (ข้อมูลการขาย) [<?php echo $saleNum; ?>]</td>
        </tr>
        <tr>
        <td class="center"><input type="checkbox" name="check[]" value="sale_report" /></td>
        <td class="left" style="padding-left: 20px;">sale_report (ออกรายงาน 1) [<?php echo $sale_report_Num; ?>]</td>
        </tr>
         <tr>
        <td class="center"><input type="checkbox" name="check[]" value="sale_report2" /></td>
        <td class="left" style="padding-left: 20px;">sale_report2 (ออกรายงาน 2) [<?php echo $sale_report2_Num; ?>]</td>
        </tr>
         <tr>
        <td class="center"><input type="checkbox" name="check[]" value="debtor_report" /></td>
        <td class="left" style="padding-left: 20px;">debtor_report (รายงานชำระหนี้) [<?php echo $debtor_report_Num; ?>]</td>
        </tr>
         <tr>
        <td class="center"><input type="checkbox" name="check[]" value="debtor_total" /></td>
        <td class="left" style="padding-left: 20px;">debtor_total (ข้อมูลลูกหนี้หลัก) [<?php echo $debtor_total_Num; ?>]</td>
        </tr>
         <tr>
        <td class="center"><input type="checkbox" name="check[]" value="debtor" /></td>
        <td class="left" style="padding-left: 20px;">debtor (ข้อมูลลูกหนี้) [<?php echo $debtor_Num; ?>]</td>
        </tr>
         <tr>
        <td class="center"><input type="checkbox" name="check[]" value="bill" /></td>
        <td class="left" style="padding-left: 20px;">bill (ข้อมูลบิล) [<?php echo $bill_Num; ?>]</td>
        </tr>
        </tbody>
      </table>
      <?php echo "<br><font color=\"red\"><center>$save</center></font>"; ?>
  </div>
</div>
</form>
</body>
<?php mysql_close(); ?>
</html>
